﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Web.Security;

namespace PB.FBConnect
{
    public class CustomIdentity : IIdentity
    {

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Facebook"; }
        }

        public bool IsAuthenticated
        {
            get
            {
                FBSession fbSession = new FBSession();
                if (fbSession.IsConnected())
                {
                    if (fbSession.UserID == FacebookUserId)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
                
            }
        }

        public string Name
        {
            get { return FacebookUserId.ToString(); }
        }

        #endregion

        #region Public Properties

        #region Auto-implemented Properties
        public long FacebookUserId { get; set; }
        public bool IsLocked { get; set; }
        public string DisplayName { get; set; }
        public string Picture { get; set; }
        public DateTime StartDate { get; set; }
        #endregion

        #region Defined Properties
        public bool IsUserRegistered
        {
            get
            {
                return FBMembership.IsUserRegistered(FacebookUserId);
            }
        }
        public string[] UserRoles
        {
            get
            {
                return Roles.GetRolesForUser(FacebookUserId.ToString());
            }
            set
            {
                if (value != null && value.Length > 0)
                    Roles.AddUserToRoles(FacebookUserId.ToString(), value);
            }
        }
        #endregion

        #endregion

        #region Constructors
        public CustomIdentity()
        {

        }

        public CustomIdentity(long uid, string dispName, string pictureUrl, bool isLocked, DateTime startDt)
        {
            FacebookUserId = uid;
            DisplayName = dispName;
            Picture = pictureUrl;
            IsLocked = isLocked;
            StartDate = startDt;
        }

        #endregion
    }
}
